package com.epam.training.domain;

import java.io.Serializable;
import java.math.BigDecimal;

import javax.persistence.*;
import javax.validation.constraints.Pattern;

import java.util.List;

/**
 * The persistent class for the car database table.
 * 
 */
@Entity
@Table(name = "car")
@NamedQuery(name = "Car.findAll", query = "SELECT c FROM Car c")
public class Car implements Serializable {
	private static final long serialVersionUID = 1L;

	@Id
	@SequenceGenerator(name = "CAR_CARID_GENERATOR", sequenceName = "CAR_ID_SEQ", initialValue = 1, allocationSize = 1)
	@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "CAR_CARID_GENERATOR")
	@Column(name = "car_id", insertable = false, updatable = false, unique = true, nullable = false)
	private Integer carId;

	@Column(length = 10)
	private String color;

	private Integer consumption;

	@Column(name = "engine_volume")
	private double engineVolume;

	@Column(length = 6)
	@Pattern(regexp = "auto|manual")
	private String gearbox;

	@Column(length = 100)
	private String model;

	@Column(name = "power")
	private Integer power;

	@Column(name = "tank_capacity")
	private Integer tankCapacity;

	@Column(length = 50)
	private String year;

	// @Column(length = 7, nullable = false)
	// private String lock;

	@Column(nullable = false)
	private BigDecimal price;

	// bi-directional many-to-one association to Bookingitem
	@OneToMany(mappedBy = "car")
	private List<BookingItem> bookingItems;

	public Car() {
	}

	/**
	 * @return the lock
	 */
	// public String getLock() {
	// return lock;
	// }

	/**
	 * @param lock
	 *            the lock to set
	 */
	// public void setLock(String lock) {
	// this.lock = lock;
	// }

	public Integer getCarId() {
		return this.carId;
	}

	public void setCarId(Integer carId) {
		this.carId = carId;
	}

	public String getColor() {
		return this.color;
	}

	public void setColor(String color) {
		this.color = color;
	}

	public Integer getConsumption() {
		return this.consumption;
	}

	public void setConsumption(Integer consumption) {
		this.consumption = consumption;
	}

	public double getEngineVolume() {
		return this.engineVolume;
	}

	public void setEngineVolume(double engineVolume) {
		this.engineVolume = engineVolume;
	}

	public String getGearbox() {
		return this.gearbox;
	}

	public void setGearbox(String gearbox) {
		this.gearbox = gearbox;
	}

	public String getModel() {
		return this.model;
	}

	public void setModel(String model) {
		this.model = model;
	}

	public Integer getPower() {
		return this.power;
	}

	public void setPower(Integer power) {
		this.power = power;
	}

	public Integer getTankCapacity() {
		return this.tankCapacity;
	}

	public void setTankCapacity(Integer tankCapacity) {
		this.tankCapacity = tankCapacity;
	}

	public String getYear() {
		return this.year;
	}

	public void setYear(String year) {
		this.year = year;
	}

	/**
	 * @return the price
	 */
	public BigDecimal getPrice() {
		return price;
	}

	/**
	 * @param price
	 *            the price to set
	 */
	public void setPrice(BigDecimal price) {
		this.price = price;
	}

	public List<BookingItem> getBookingItems() {
		return this.bookingItems;
	}

	public void setBookingitems(List<BookingItem> bookingItems) {
		this.bookingItems = bookingItems;
	}

	public BookingItem addBookingitem(BookingItem bookingItem) {
		getBookingItems().add(bookingItem);
		bookingItem.setCar(this);

		return bookingItem;
	}

	public BookingItem removeBookingitem(BookingItem bookingItem) {
		getBookingItems().remove(bookingItem);
		bookingItem.setCar(null);

		return bookingItem;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((bookingItems == null) ? 0 : bookingItems.hashCode());
		result = prime * result + ((carId == null) ? 0 : carId.hashCode());
		result = prime * result + ((color == null) ? 0 : color.hashCode());
		result = prime * result
				+ ((consumption == null) ? 0 : consumption.hashCode());
		long temp;
		temp = Double.doubleToLongBits(engineVolume);
		result = prime * result + (int) (temp ^ (temp >>> 32));
		result = prime * result + ((gearbox == null) ? 0 : gearbox.hashCode());
		result = prime * result + ((model == null) ? 0 : model.hashCode());
		result = prime * result + ((power == null) ? 0 : power.hashCode());
		result = prime * result
				+ ((tankCapacity == null) ? 0 : tankCapacity.hashCode());
		result = prime * result + ((year == null) ? 0 : year.hashCode());
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Car other = (Car) obj;
		if (bookingItems == null) {
			if (other.bookingItems != null)
				return false;
		} else if (!bookingItems.equals(other.bookingItems))
			return false;
		if (carId == null) {
			if (other.carId != null)
				return false;
		} else if (!carId.equals(other.carId))
			return false;
		if (color == null) {
			if (other.color != null)
				return false;
		} else if (!color.equals(other.color))
			return false;
		if (consumption == null) {
			if (other.consumption != null)
				return false;
		} else if (!consumption.equals(other.consumption))
			return false;
		if (Double.doubleToLongBits(engineVolume) != Double
				.doubleToLongBits(other.engineVolume))
			return false;
		if (gearbox == null) {
			if (other.gearbox != null)
				return false;
		} else if (!gearbox.equals(other.gearbox))
			return false;
		if (model == null) {
			if (other.model != null)
				return false;
		} else if (!model.equals(other.model))
			return false;
		if (power == null) {
			if (other.power != null)
				return false;
		} else if (!power.equals(other.power))
			return false;
		if (tankCapacity == null) {
			if (other.tankCapacity != null)
				return false;
		} else if (!tankCapacity.equals(other.tankCapacity))
			return false;
		if (year == null) {
			if (other.year != null)
				return false;
		} else if (!year.equals(other.year))
			return false;
		return true;
	}
}
